package map
{
	import findPath.Cell;
	import findPath.NavMesh;
	import findPath.Triangle;
	
	import flash.geom.Point;
	
	public class MapInfo
	{
		public static var width:int;  //实际显示宽
		public static var height:int; //实际显示高
		public static var  resId:int; //地图目录标号
		
		//地图单张切块图片的宽度和高度
		public static const map_block_w:int = 300;
		public static const map_block_h:int = 300;
		public static var map_block_Rows:int;
		public static var map_block_Cols:int;
		
		public static var map_cache_Rows:int;
		public static var map_cache_Cols:int;
		
		//路点格子
		public static const tileWidth:int = 64; //tile的宽
		public static const tileHeight:int = 32;//tiled的高
		public static var halfTW:int = 64/2;//tile的宽/2
		public static var halfTH:int = 32/2;//tiled的高/2
		
		public static var gridWidth:int;//网格宽
		public static var gridHeight:int;//网格高
		
		public static const view_width:int = 1000;
		public static const view_height:int = 560;//显示的地图高范围
		public static const chatWidth:int = 128;
		public static const chatHeight:int = 128;
		
		//bigScreen
		//public static var sceneWidth:int = view_width //- chatWidth  ; //显示的地图宽范围
		
		// keyboardMoveSpeed
		public static const keyboardMoveSpeedX:int = 6;
		public static const keyboardMoveSpeedY:int = 3;
		
		//public static const PRE_SPEED:Number = 0.30;
		public static const PRE_SPEED:Number = 0.50;
		public static var MOVE_SPEED:int; //人物移动的速度
		
		/*public static var _preSpeedX:Number;
		public static var _preSpeedY:Number;*/
		
		public function MapInfo()
		{
		}
		
		//地图初始
		public static function init():void
		{
			MapInfo.gridWidth = MapInfo.width/MapInfo.tileWidth; 
			MapInfo.gridHeight = MapInfo.height/MapInfo.tileHeight;
			
			
			//地图切块的数据
			if(width % map_block_w == 0){
				map_block_Cols = width/map_block_w;
			}else{
				map_block_Cols = Math.floor(width/map_block_w) + 1;
			}
			
			if(height % map_block_h == 0){
				map_block_Rows = height/map_block_h;
			}else{
				map_block_Rows = Math.floor(height/map_block_h) + 1;
			}
			
			// map_cache_Rows ,map_cache_Cols
			map_cache_Cols = Math.floor(view_width / map_block_w) + 1;
			
			map_cache_Rows = Math.floor(view_height / map_block_h) + 1;
		}
		
		//========寻路相关=====================================================
		private var _startPt:Point;
		private var _endPt:Point;
		private var _cellV:Vector.<Cell>;
		
		private function setCellV(triangleV:Vector.<Triangle>):void{
			//构建寻路数据
			_cellV = new Vector.<Cell>();
			var trg:Triangle;
			var cell:Cell;
			for (var j:int=0; j<triangleV.length; j++) {
				trg = triangleV[j];
				cell = new Cell(trg.getVertex(0), trg.getVertex(1), trg.getVertex(2));
				cell.index = j;
				_cellV.push(cell);
			}
			linkCells(_cellV);
		}
		//返回拐点路径
		public function findPath(starX:int,starY:int,endX:int,endY:int):Array{
			
			_startPt = new Point(starX, starY);
			_endPt = new Point(endX, endY);
			
			var nav:NavMesh = new NavMesh(_cellV);
			return nav.findPath(_startPt, _endPt);
		}
		
		private function linkCells(pv:Vector.<Cell>):void {
			for each (var pCellA:Cell in pv) {
				for each (var pCellB:Cell in pv) {
					if (pCellA != pCellB) {
						pCellA.checkAndLink(pCellB);
					}
				}
			}
		}
	}
}